package XO;
import com.sun.org.apache.xpath.internal.operations.*;
import sun.plugin2.message.Message;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.String;
import javax.swing.*;
/**
 * Created by IntelliJ IDEA.
 * User: eagle
 * Date: 19.08.11
 * Time: 10:20
 * To change this template use File | Settings | File Templates.
 */
public class XOForm extends JFrame implements ActionListener {

	//private variables

	private JMenuBar 	menuBar;
	private JMenu 		gameMenu;

	private JMenu		gameFindMenu;
	private JMenuItem	gameFindLan;
	private JMenuItem	gameFindAir;
	private JMenu		gameHostMenu;
	private JMenuItem	gameHostLan;
	private JMenuItem	gameHostAir;
	private JMenuItem	gameExit;
	private GridLayout  grid;

	private int fieldSize;
	private int chainLength;

	public XOForm(){
		super();
		initComponents();
	}

	public void actionPerformed(ActionEvent ae){
		//JOptionPane.showMessageDialog(this, ">" + ae.getActionCommand().toString() + "<");
		//JOptionPane.showMessageDialog(this, ">" + ae.getActionCommand() + "<");
//		if(ae.getActionCommand().equals("Exit"))
//			JOptionPane.showMessageDialog(null,"EXIT");
//		else
//			JOptionPane.showMessageDialog(null,"AAAA");
		//fillGameField(3,3);
		//String command = ae.getActionCommand().toString();
		String command = ae.getActionCommand().toString();
		if(command == "Find LAN"){

		}   else if (command == "Find Air"){

		}   else if(command == "Host LAN"){
			//fillGameField(3,3);
			hostGame(HostType.LAN);
		}   else if(command == "Host Air"){
			//fillGameField(3,3);
			hostGame(HostType.Air);
		}	else if(command == "Exit"){
			this.dispose();
		}
	}


	private void initComponents(){
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.fieldSize = 0;
		this.chainLength = 0;

		this.setSize(480,300);

		menuBar = new JMenuBar();
		menuBar.setName("MainMenu");

		gameMenu = new JMenu("Game");

		gameFindMenu = new JMenu("Find");
		gameFindLan = new JMenuItem("LAN");
		gameFindLan.setActionCommand("Find LAN");
		gameFindLan.addActionListener(this);
		gameFindAir = new JMenuItem("Air");
		gameFindAir.setActionCommand("Find Air");
		gameFindAir.addActionListener(this);
		gameFindMenu.add(gameFindLan);
		gameFindMenu.add(gameFindAir);
		gameMenu.add(gameFindMenu);

		gameHostMenu = new JMenu("Host");
		gameHostLan = new JMenuItem("LAN");
		gameHostLan.setActionCommand("Host LAN");
		gameHostLan.addActionListener(this);
		gameHostAir = new JMenuItem("Air");
		gameHostAir.setActionCommand("Host Air");
		gameHostAir.addActionListener(this);
		gameHostMenu.add(gameHostLan);
		gameHostMenu.add(gameHostAir);
		gameMenu.add(gameHostMenu);

		gameMenu.addSeparator();
		gameExit = new JMenuItem("Exit");
		gameExit.addActionListener(this);
		gameMenu.add(gameExit);

		menuBar.add(gameMenu);
		this.setJMenuBar(menuBar);

		grid = new GridLayout();
		this.getContentPane().setLayout(grid);
		this.getContentPane().setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

		this.setVisible(true);
	}

	private void findGame(){

	}

	private void hostGame(HostType hostType){
//		XOHostDialog dlg = new XOHostDialog(this);
//		dlg.setVisible(true);
//		fillGameField(dlg.getFieldSize(), dlg.getChainLength());

		//тут как-то надо обработать тип хоста (ЛАН или радио)
		Object [] comps = {new JLabel("Field Size"), new JTextField("3",3), new JLabel("Chain Length"), new JTextField("3",3), "Ok", "Cancel" };
		int res = JOptionPane.showOptionDialog(this, null, "Parametrize",JOptionPane.CLOSED_OPTION, JOptionPane.DEFAULT_OPTION, null,comps,null);

		switch (res)
		{
			case 4:
				//JOptionPane.showMessageDialog(this, "OK");
				try
				{
					this.fieldSize = Integer.parseInt(((JTextField)comps[1]).getText());
					this.chainLength = Integer.parseInt(((JTextField)comps[3]).getText());
				}
				catch (Exception exx)
				{
					JOptionPane.showMessageDialog(this, exx.getMessage());
					break;
				}
				fillGameField();
				break;
			case 5:
				//Cancel
				break;
			case JOptionPane.CLOSED_OPTION:
				//ClosedOption
				break;
		}

		
	}

	//private void fillGameField(int rows, int cols){
	private void fillGameField(){
		this.getContentPane().removeAll();
		
		grid.setRows(this.fieldSize);
		grid.setColumns(this.fieldSize);

		//this.getContentPane().setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        this.getContentPane().setLayout(grid);
		for (int i=0; i<fieldSize*fieldSize; i++){
			JButton button = new JButton();
			button.setActionCommand("Cell " + i);
			button.addActionListener(this);
			this.getContentPane().add(button);
		}
		this.getContentPane().validate();

		for(Component c : this.getContentPane().getComponents())
		{
			((JButton)c).addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					//To change body of implemented methods use File | Settings | File Templates.
					checkTheButton((JButton)e.getSource());
				}
			});
		}
		//int a = this.getContentPane().getComponents().length;
		//JOptionPane.showMessageDialog(this, a);
//		this.getContentPane().setVisible(false);
//		this.getContentPane().setVisible(true);
	}

	private  void checkTheButton(JButton button)
	{
		JOptionPane.showMessageDialog(this, button.getActionCommand());
	}

	//болтовня
	private void shHi(){

	}

	private void shBye(){

	}

	private void shNewGame(){

	}

	private void shSend(){

	}

	private void shAi(){

	}

	private void shWhoIs(){

	}

	private void shReceive(){

	}

	public enum HostType {
		LAN, Air
	}
}
